package com.shqcjd.yunweiyanoperator.ui.activity.staff;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.ContactsNewsBean;
import com.shqcjd.yunweiyanoperator.ui.adapter.ContactsAddMsgAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.WrapContentLinearLayoutManager;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 好友添加消息
 */
public class ContactsAddMsgActivity extends BaseActivity {


    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_contacts_news_manage)
    RecyclerView rvContactsNewsManage;
    @BindView(R.id.sr_contacts_news_manage)
    SwipeRefreshLayout srContactsNewsManage;
    @BindView(R.id.rl_isnull)
    RelativeLayout rlIsnull;

    private Context mContext;
    List<ContactsNewsBean> mList = new ArrayList<>();
    private ContactsAddMsgAdapter contactsNewsAdapter;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_contacts_add_msg;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle.setText("新的朋友");
        refreshData();
        srContactsNewsManage.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });

    }

    private void refreshData() {
        srContactsNewsManage.setRefreshing(true);
        mList.clear();
        getDataList();
    }


    private void getDataList() {
        Map<String, String> map = new HashMap<>();
        ApiService.contactsRequest(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<List<ContactsNewsBean>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<List<ContactsNewsBean>> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }
                        mList.addAll(responseData.data);
                        if (contactsNewsAdapter == null) {
                            rvContactsNewsManage.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
                            contactsNewsAdapter = new ContactsAddMsgAdapter(mList, mContext);
                            rvContactsNewsManage.setAdapter(contactsNewsAdapter);
                            contactsNewsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    if(mList.get(position).friendStatus==1){
                                        addContracts(position);
                                    }
                                }
                            });
                        }
                        if (mList.size() == 0) {
                            rlIsnull.setVisibility(View.VISIBLE);
                        } else {
                            rlIsnull.setVisibility(View.INVISIBLE);
                        }
                        contactsNewsAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        srContactsNewsManage.setRefreshing(false);
                    }
                });
    }

    private void addContracts(int postion) {
        Map<String, String> map = new HashMap<>();
        map.put("friendId", mList.get(postion).friendId+"");
        map.put("friendStatus", "2");
        map.put("friendAppCode", mList.get(postion).friendAppCode);
        ApiService.updateFriendStatus(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("添加中");
                    }

                    @Override
                    public void _onNext(ResponseData responseData) {
                        if (responseData.retCode == 10000) {
                            mList.remove(postion);
                            contactsNewsAdapter.notifyItemChanged(postion);
                            if (mList.size() == 0) {
                                rlIsnull.setVisibility(View.VISIBLE);
                            } else {
                                rlIsnull.setVisibility(View.INVISIBLE);
                            }
                            ToastUtils.showShort(mContext, "添加成功");
                        }else{
                            ToastUtils.showShort(mContext, responseData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }
}
